﻿@using KPIS.GERP.Models.ICM
@model IEnumerable<KPIS.GERP.Models.ICM.Dispense>

@{
    ViewBag.Title = "จ่ายพัสดุ [DP]";
}

@using (Html.BeginForm("DeleteAll", "Dispense", FormMethod.Post, new { @id = "form" }))
{
    @Html.AntiForgeryToken()
    <div class="row-fluid">
        <div class="block-button-menu-above">
            @Html.ButtonAdd()
        </div>

        @Html.ValidationSummary(true, "", new { @class = "alert alert-error" })

    <div class="row-fluid">
    <div class="table-header span12">
        <div class="span10">
            @ViewBag.Title
        </div>
        @Html.Partial("_ExportButton")
    </div>
</div>


    <table id="tblDispense" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
        <thead>
            <tr>
                <th class="center">
                    เลขที่การจ่าย
                </th>
                <th style="text-align:center; width:80px;">@Html.DisplayNameFor(model => model.DispenseDate)</th>
                <th style="text-align: center; width: 120px;">@Html.DisplayNameFor(model => model.DispenseName)</th>
                <th style="text-align: center; width: 90px;">เลขที่ใบเบิก</th>
                <th style="text-align: center; width: 80px;">@Html.DisplayNameFor(model => model.WhId)</th>
                <th style="text-align: center; width: 120px;">@Html.DisplayNameFor(model => model.FromJobSiteId)</th>
                <th style="text-align: center;">@Html.DisplayNameFor(model => model.ProjectId)</th>
                <th style="text-align: center; width: 50px;">จำนวนรายการขอเบิก</th>
                <th style="text-align: center;">จำนวนรายการจ่ายแล้ว</th>
                <th>&nbsp;</th>
            </tr>
        </thead>

        <tbody>
            @foreach(var m in Model)
            {
                <tr>
                    <td class="center">
                        @m.Code
                    </td>

                <td>@(m.DispenseDate==null ? "-":m.DispenseDate.Value.ToString("d/M/yyy"))</td>
                <td>@Html.ActionLink(@m.DispenseName, "Details", new { id=m.DispenseId })</td>
                    <td><a href="@Url.Action("Details", "RequestParcel", new {id=m.RequestParcelId})" target="_blank">
                            @(m.RequestParcel.IcmReqCode)<i class="icon-external-link"></i>
                        </a></td>
                <td>@Html.DisplayFor(modelItem => m.Warehouse.Name)</td>
                <td>@Html.DisplayFor(modelItem => m.FromJobSite.Name)</td>  
                <td>@Html.DisplayFor(modelItem => m.Project.Name)</td> 
                <td style="text-align:center">
                    @{
                        KPIS.GERP.Models.GERPContext db = new KPIS.GERP.Models.GERPContext();
                        var dtl = (from a in db.RequestParcelDtls
                                   where a.ReqId == m.RequestParcelId && a.RecordStatus !="D"
                                   select a).ToList();
                                   
                        var Disp = dtl.Select(x=>x.DispenseStatus).ToList();     
                        var DispCount = dtl.Count();
                             
                        int Dispense = 0;
                        
                        for(int i=0; i<DispCount; i++)
                        {
                            if (Disp[i] == "true")
                            {
                                Dispense = Dispense + 1;
                            }
                        }
                        ViewBag.SumDispense = Dispense;
                        ViewBag.TotalDispenseItem = dtl.Count();
                     }
                    @ViewBag.TotalDispenseItem
                </td>
                <td style="text-align:center">@ViewBag.SumDispense</td>

                <td class="td-actions">
                    <div class="hidden-phone visible-desktop btn-group">
                        @Html.ButtonDetails(new { id = m.DispenseId}, true)
                        @Html.ButtonEdit(new { id = m.DispenseId }, true)
                        @*@Html.ButtonDelete(new { id = m.DispenseId }, true)
                        @{
                var service = new KPIS.GERP.WEB.GIMS.RequestParcelService.RequestParcelServiceClient();
                var item = service.SelectRequestParcelDtl(new RequestParcelDtl { ReqId = m.RequestParcelId, RecordStatus ="!D" }, null).ToList().Count();
                    if (item > 0)
                    { 
                        <a href="#" role="button" class="btn btn-danger btn-mini" aria-disabled="true" onclick="DelEvent()">
                            <i class="icon-trash bigger-120"></i>
                        </a>
                    }
                    else
                    {
                        @Html.ButtonDelete(new { id = m.DispenseId }, true)
                    }
                
                }*@
                    </div>
                </td>
            </tr>
            }

        </tbody>
    </table>
</div>
}


@section scripts {

    <script type="text/javascript">

        //function DelEvent() {
        //    bootbox.alert("ไม่สามารถลบรายการนี้ได้ เนื่องจากในรายการมีรายการที่ต้องทำการจ่าย");
        //}

        $(function () {
            var oTable1 = $('#tblDispense').dataTable({
                "aoColumns": [
                  { "bSortable": false },
                  null, null, null, null, null, null, null, null,
                  { "bSortable": false }
                ]
            });

            $('table th input:checkbox').on('click', function () {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
                .each(function () {
                    this.checked = that.checked;
                    $(this).closest('tr').toggleClass('selected');
                });

            });

            $('[data-rel=tooltip]').tooltip();


            $("#form").submit(function (e) {
                e.preventDefault();
                var currentForm = this;
                bootbox.confirm("คุณแน่ใจหรือไม่", function (c) {
                    if (c) {
                        $.ajax({
                            url: "@Url.Action("DeleteAll")",
                        type: "post",
                        dataType: "json",
                        data: $(currentForm).serialize(),
                        context: document.body,
                        success: function (s) {
                            if (s.success) {
                                location.reload();
                            }
                        },
                        error: function (xhr, status, error) {
                            alert("Status: " + status + ", Error: " + error + ", XHR: " + xhr.readyState);
                        },
                        statusCode: {
                            404: function () {
                                alert("Page not found");
                            }
                        }
                    });
                }
            })
            return false;
        });

        })
        
    </script>

}

